//
//  ViewController.m
//  09-UITableView省市区作业
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *nameArray;
@property (nonatomic,strong) NSMutableArray *secetNameArray;
@property (nonatomic,strong) NSMutableArray *seceIndexArray;
@property (nonatomic,strong) NSMutableArray *provinceArr;
@property (nonatomic,strong) NSMutableArray *provinceArr1;
@property (nonatomic,strong) NSMutableArray *cityArray;
@property (nonatomic,strong) NSMutableArray *cityArray1;
//@property (nonatomic,strong) NSMutableArray *cityArr1;
//@property (nonatomic,strong) NSMutableArray *zoneArr1;
@property (nonatomic,strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    1.在昨天作业基础上, 添加联系人删除功能
//    2.编写省 推出-> 市 推出-> 区
//    3.编写分区是省, cell是市, 点击市推出区的页面
    NSArray *array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];

    _seceIndexArray = [NSMutableArray array];
    _secetNameArray = [NSMutableArray array];
    _nameArray = [NSMutableArray arrayWithArray:array];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.view addSubview:_tableView];
    
//    _button = [UIButton buttonWithType:UIButtonTypeCustom];
//    _button.backgroundColor = [UIColor redColor];
//    _button.frame = CGRectMake(40, _tableView.rowHeight, 50, 30);
    
//        NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-UITableView省市区作业/09-UITableView省市区作业/area.txt";
//    NSString *source = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    //     NSLog(@"%@",source);
//    NSArray *arr = [source componentsSeparatedByString:@"\n"];
//    //     NSLog(@"%@",arr);
//    _provinceArr = [NSMutableArray array];
//    _cityArr = [NSMutableArray array];
//    _zoneArr = [NSMutableArray array];
    
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-UITableView省市区作业/09-UITableView省市区作业/area.txt";
    NSString *areaSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"%@",areaSource);
    
    NSArray *lineArr = [areaSource componentsSeparatedByString:@"\n"];
    
    //    NSLog(@"%@",lineArr);
   _provinceArr1 = [NSMutableArray array];
    NSMutableArray *provinceArr = [NSMutableArray array];
    for (NSString *line in lineArr) {
        if ([line hasPrefix:@" "] == NO) {
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            [provinceDic setObject:line forKey:@"ProvinceName"];
            NSMutableArray *cityArr = [NSMutableArray array];
            [provinceDic setObject:cityArr forKey:@"cityGroup"];
            [provinceArr addObject:provinceDic];
            [_provinceArr1 addObject:line];
//             NSLog(@"%@",line);
//             NSLog(@"%@",_provinceArr1);
            //            NSLog(@"%@",provinceDic);
            //            NSLog(@"%@",provinceArr);
            //区所有的市
        }else if ( [line hasPrefix:@"  "] == YES && [line hasPrefix:@"    "] == NO){
            //创建市字典（name和区array）
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:[line substringFromIndex:2] forKey:@"cityName"];
            NSMutableArray *districtArr = [NSMutableArray array];
            [cityDic setObject:districtArr forKey:@"districtGroup"];//
            [provinceArr.lastObject[@"cityGroup"] addObject:cityDic];
            //            NSLog(@"%@",cityDic);
            //            NSLog(@"%@",districtArr);
//                        NSLog(@"%@",provinceArr);
        }
        //取到所有的区/县
        else{
            //最后一个市
            //通过省数组获取到当前所在省的字典
            [[[provinceArr lastObject][@"cityGroup"] lastObject][@"districtGroup"]addObject:[line substringFromIndex:4]];//最终将区、县添加到数组中
        }
    }
//    NSLog(@"%@",provinceArr);
//    NSLog(@"%@",_provinceArr1);
    _cityArray = [NSMutableArray array];
//    for (NSDictionary *dic in provinceArr) {
//         _cityArray = [dic objectForKey:@"cityGroup"];
//        
//        }
//    }
//    NSLog(@"%@",_cityArray);
    
//    for (NSDictionary *dic in _cityArray) {
//        NSString *str = [dic objectForKey:@"cityName"];
////         NSLog(@"%@",str);
//        [_cityArray1 addObject:str];
//    }
//    og(@"%@",_cityArray1);

    //
//    NSLog(@"%@",_cityArray);
    
//    for (NSString *str in arr) {
//        if (![str hasPrefix:@" "]) {
//            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
//            NSMutableArray *cityArr = [NSMutableArray array];
//            [provinceDic setObject:str forKey:@"provinceName"];
//            [provinceDic setObject:cityArr forKey:@"cityGroup"];
//            [_provinceArr addObject:provinceDic];
//
//        }
//        if ([str hasPrefix:@"  "] && ![str hasPrefix:@"    "]) {
//            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
//            NSMutableArray *zoneArr = [NSMutableArray array];
//            [cityDic setObject:str forKey:@"cityName"];
//            [cityDic setObject:zoneArr forKey:@"zoneGroup"];
//            [[[_provinceArr lastObject]objectForKey:@"cityGroup"] addObject:cityDic];
//        } else {
//            [[[[[_provinceArr lastObject] objectForKey:@"cityGroup"] lastObject] objectForKey:@"zoneGroup"] addObject:str];
//        }
//    }
//    NSLog(@"%@",_provinceArr);
}

    
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (self.editing) {
//        [_secetNameArray removeObject:_nameArray[indexPath.row]];
//        [_seceIndexArray removeObject:indexPath];
//        
//    }
//}

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [_nameArray removeObjectAtIndex:indexPath.row];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//    }
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return _nameArray.count;
    return _provinceArr1.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
//    cell.textLabel.text = _nameArray[indexPath.row];
   
    cell.textLabel.text = _provinceArr1[indexPath.row];
    

   
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
